# pip install evdev
from evdev import InputDevice, categorize, ecodes

# 找到键盘设备
devices = [InputDevice(path) for path in evdev.list_devices()]
keyboard_device = None
for device in devices:
    if "keyboard" in device.name.lower():
        keyboard_device = device
        break

if not keyboard_device:
    print("未找到键盘设备！")
    exit()

print(f"监听键盘设备: {keyboard_device.name}")

# 监听键盘事件
for event in keyboard_device.read_loop():
    if event.type == ecodes.EV_KEY:
        key_event = categorize(event)
        if key_event.keystate == 1:  # 按下事件
            if key_event.keycode == "KEY_G" and "KEY_LEFTCTRL" in keyboard_device.active_keys():
                print("Ctrl+G 被按下，程序退出！")
                break